function formatTime() {
  let date = new Date()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  let d = date.getDate()
  let h = date.getHours()
  let minu = date.getMinutes()
  let s = date.getSeconds()
  let flag = 'AM'
  if (h > 12) {
    h -= 12
    flag = 'PM'
  }
  let day = date.getDay()
  // 对arr数组元素重新处理，生成一个新数组，元素是加0的字符串
  let arr = [y, m, d, h, minu, s, day].map(formatNumber)
  console.log(arr)
  return `${arr[0]}年${arr[1]}月${arr[2]}日 ${arr[3]}:${arr[4]}:${arr[5]} ${flag} ${getDayOfWeek(day)}`
}
// 不够2位数补0
function formatNumber(n) {
  // 把n转换为字符串
  n = String(n) // '10'
  return n[1] ? n : `0${n}`
}
// 传递参数获得周中的天
function getDayOfWeek(n) {
  let week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
  return week[n - 1]
}

setInterval(function () {
  document.querySelector('.date').innerHTML = formatTime()
}, 1000)
